﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using AudioCore.Biz;
using AudioCore.Entity;

namespace AudioBook.AudioManagement.AudioManage
{
    public partial class Bin : System.Web.UI.Page
    {
        #region Fields & constants

        ArticleBiz bizArticle = new ArticleBiz();
        CategoryBiz bizCategoryArticle = new CategoryBiz();
        List<CategoryEntity> lstCategory;

        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
            LoadArticle();
        }

        private void LoadArticle()
        {
            int recordCount = 0;
            List<ArticleEntity> lstArticle;

            lstArticle = bizArticle.GetDeletedArticle(grid.CurrentPageIndex, grid.PageSize, out recordCount);

            grid.VirtualItemCount = recordCount;
            grid.DataSource = lstArticle;
            grid.DataBind();
        }

        private bool ChangeActive(bool active)
        {
            foreach (DataGridItem item in grid.Items)
            {
                CheckBox chkSelected = (CheckBox)item.FindControl("chkSelected");
                if (chkSelected.Checked)
                {
                    HiddenField hidArticleID = (HiddenField)item.FindControl("hidArticleID");
                    int ArticleID = int.Parse(hidArticleID.Value);
                    ArticleEntity enArticle = bizArticle.GetArticleByID(ArticleID);
                    if (enArticle != null)
                    {
                        if (enArticle.Actived != active)
                        {
                            enArticle.Actived = active;
                            bizArticle.UpdateArticle(enArticle);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        #endregion Private methods

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }

        protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ArticleEntity enArticle = (ArticleEntity)e.Item.DataItem;

                Label lblOrder = (Label)e.Item.FindControl("lblOrder");
                lblOrder.Text = (e.Item.ItemIndex + 1).ToString();

                HiddenField hidArticleID = (HiddenField)e.Item.FindControl("hidArticleID");
                hidArticleID.Value = enArticle.ArticleID.ToString();

                HyperLink hplName = (HyperLink)e.Item.FindControl("hplName");
                hplName.Text = enArticle.ArticleName;
                hplName.NavigateUrl = string.Format(GUIConstants.WebPage.AudioPage.ArticleDetail, enArticle.ArticleID);

                ImageButton btnActive = (ImageButton)e.Item.FindControl("btnActive");
                btnActive.ImageUrl = enArticle.Actived ? GUIConstants.Icon.TrueIcon : GUIConstants.Icon.FalseIcon;

                Label lblRate = (Label)e.Item.FindControl("lblRate");
                //lblRate.Text = enArticle.RateTimes == 0 ? string.Empty : ((float)enArticle.RatePoint / enArticle.RateTimes).ToString();

                Label lblPlayCounter = (Label)e.Item.FindControl("lblPlayCounter");
                lblPlayCounter.Text = enArticle.PlayCounter.ToString();

                Label lblCreatedDateTime = (Label)e.Item.FindControl("lblCreatedDateTime");
                lblCreatedDateTime.Text = enArticle.CreatedDateTime.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                Label lblLastUpdateDateTime = (Label)e.Item.FindControl("lblLastUpdateDateTime");
                lblLastUpdateDateTime.Text = enArticle.LastTimeUpdate.ToString(GUIConstants.Common.DATE_FORMAT);

                Label lblArticleID = (Label)e.Item.FindControl("lblArticleID");
                lblArticleID.Text = enArticle.ArticleID.ToString();

                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = string.Format(GUIConstants.WebPage.AudioPage.EditArticle, enArticle.ArticleID);
            }
        }

        protected void grid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grid.CurrentPageIndex = e.NewPageIndex;
            LoadArticle();
        }

        #endregion Event handlers
    }
}
